package com.zsh.boot;

import ch.qos.logback.core.db.DBHelper;
import com.zsh.boot.bean.Pet;
import com.zsh.boot.bean.User;
import com.zsh.boot.config.MyConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * @author zsh
 * @Description
 * @create 2022-02-13 10:04
 */

/**
 * 主程序类
 * 这是一个springboot应用
 */
@SpringBootApplication(scanBasePackages = "com.zsh")
public class MainApplication {

    public static void main(String[] args) {
        //1.返回IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);

        //2.可以查看所有组件
        String[] beanDefinitionNames = run.getBeanDefinitionNames();
        for(String name:beanDefinitionNames){
            System.out.println(name);
        }

        //3.从容器中获得组件
        Pet tom01 = run.getBean("cat",Pet.class);
        Pet tom02 = run.getBean("cat",Pet.class);
        System.out.println("组件" + (tom01 == tom02));

        //4.com.zsh.boot.config.MyConfig$$EnhancerBySpringCGLIB$$89fcc49@210f0cc1 代理对象
        MyConfig bean = run.getBean(MyConfig.class);
        System.out.println(bean);

        //如果@Configuration(proxyBeanMethods = true) 代理对象调用方法，SpringBoot总会检测这个组件是否在容器中
        //保持组件单实例
        //如果@Configuration(proxyBeanMethods = false)
        User user01 = bean.user01();
        User user02 = bean.user01();
        System.out.println(user01 == user02);

        User user1 = run.getBean("user01",User.class);
        System.out.println(user1==user01);
        Pet tom = run.getBean("cat",Pet.class);
        System.out.println("用户宠物：" + (user1.getPet() == tom));


        //5.获取import 组件
        String[] beanNamesForType = run.getBeanNamesForType(User.class);
        System.out.println("=======================");
        for (String s : beanNamesForType) {
            System.out.println(s);
        }

        DBHelper bean1 = run.getBean(DBHelper.class);
        System.out.println(bean1);

        System.out.println("===========================");
        System.out.println(run.containsBean("cat"));
        System.out.println(run.containsBean("user01"));

        boolean haha = run.containsBean("haha");
        boolean hehe = run.containsBean("hehe");
        System.out.println(haha);
        System.out.println(hehe);

    }
}
